import Swal from 'sweetalert2/dist/sweetalert2.js';
import 'sweetalert2/src/sweetalert2.scss';
import './style.scss';

export enum PopType {
  success = 'success',
  error = 'error',
  info = 'info',
  question = 'question',
  text = '',
}

export interface PopOtherQuery {
  onClose?: () => void;
}

class YBPopView {

  public static msgSuccess(title: string, other: PopOtherQuery = {}) {
    this._showPop(title, PopType.success, other);
  }

  public static msgError(title: string, other: PopOtherQuery = {}) {
    this._showPop(title, PopType.error, other);
  }

  public static showInfo(title: string, other: PopOtherQuery = {}) {
    this._showPop(title, PopType.info, other);
  }

  public static showQuestion(title: string, other: PopOtherQuery = {}) {
    this._showPop(title, PopType.question, other);
  }

  public static showText(title: string, other: PopOtherQuery = {}) {
    this._showPop(title, PopType.text, other);
  }

  // 加载中
  public static showLoading(title: string, other: PopOtherQuery = {}) {
    Swal.fire({
      title,
      ...other,
      allowOutsideClick: false,
      onBeforeOpen: () => {
        Swal.showLoading();
      },
    });
  }

  // 关闭加载提示
  public static hideLoading() {
    if (Swal.isLoading()) {
      Swal.close();
    }
  }

  /**
   * alert
   * @param title
   * @param msg
   * @param type warning, error, success, info, and question
   * @returns {Promise<any>}
   */
  public static alert(title: string, msg: string, type: PopType) {
    return new Promise((resolve, reject) => {
      Swal.fire({
        title,
        text: msg,
        type,
        confirmButtonText: '确定',
      }).then(() => {
        resolve();
      }).catch((err: any) => {
        reject(err);
      });
    });
  }

  // confirm
  /**
   *
   * @param title
   * @param msg
   * @param type warning, error, success, info, and question
   * @returns {Promise<any>}
   */
  public static confirm(title: string, msg: string, other: PopOtherQuery = {}) {
    return new Promise((resolve, reject) => {
      Swal.fire({
        title,
        text: msg,
        ...other,
        showCancelButton: true,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then((result: any) => {
        if (result.value) {
          resolve();
        } else {
          reject();
        }
      }).catch((err: any) => {
        reject(err);
      });
    });
  }

  public static prompt(title: string, msg?: string, check?: (str: string) => string) {
    return new Promise((resolve, reject) => {
      Swal.fire({
        title,
        text: msg,
        input: 'text',
        showCancelButton: true,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        preConfirm: (text: string) => {
          if (check) {
            const data = check(text);
            if (data) {
              Swal.showValidationMessage(
                data,
              );
            }
          }
        },
      }).then((result: any) => {
        if (result && result.dismiss) {
          reject(result);
        } else {
          resolve(result.value);
        }
      }).catch((err: any) => {
        reject(err);
      });
    });
  }

  private static _showPop(title: string, type: PopType, other: PopOtherQuery) {
    Swal.fire({
      type,
      title,
      timer: 1500,
      showConfirmButton: false,
      width: '200px',
      ...other,
    });
  }
}

export default YBPopView;

/*
text: 详情文字

onBeforeOpen: 显示前调用
onClose: 关闭的时候调用


 */
